package com.nhjf.common.util;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class StringUtil {

	/****
	 * 检测字符串不为空，并且不等于'' 返回true表示不为空，返回false表示为空
	 * 
	 * @return
	 */
	public static boolean isNotNull(String str) {
		if (str != null && !str.trim().equals("")) {
			return true;
		}
		return false;
	}

	/**
	 * 检测字符是否是数字
	 * 
	 * @param str
	 * @return 是纯数字返回true,否则返回false;
	 */
	public static boolean isNumeric(String str) {
		for (int i = str.length(); --i >= 0;) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	/****
	 * 检测集合 List 是否为空，size 是否大于 0 返回值：true 表示不为空且size 大于 0
	 * 
	 * @return
	 */
	public static boolean isNotNull(List list) {
		if (list != null && list.size() > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 判断2个字符串是否相等
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean equal(String a, String b) {
		boolean result = false;
		if (null == a || "" == a) {
			if (null == b || "" == b) {
				result = true;
			}
		} else {
			if (null != b && a.trim().equals(b.trim())) {
				result = true;
			}
		}
		return result;
	}

	/**
	 * 转码
	 * 
	 * @return
	 */
	public static final String changeCode(String str, int unicodeFlag) {
		try {
			switch (unicodeFlag) {
			case 0:
				return str;
			case 1:
				return new String(str.getBytes("ISO-8859-1"), "gb2312");
			case 2:
				return new String(str.getBytes("gb2312"), "ISO-8859-1");
			case 3:
				return new String(str.getBytes("UTF-8"), "gb2312");
			case 4:
				return new String(str.getBytes("gb2312"), "UTF-8");
			case 5:
				return new String(str.getBytes("GBK"), "ISO-8859-1");
			case 6:
				return new String(str.getBytes("ISO-8859-1"), "GBK");
			default:
				return str;
			}
		} catch (Exception ex) {
			return "";
		}
	}

	/**
	 * 转码2
	 * 
	 * @return
	 */
	public static String getString(String s, String srcEncoding,
			String destEncoding) {
		try {
			return new String(s.getBytes(srcEncoding), destEncoding);
		} catch (UnsupportedEncodingException e) {
			return s;
		}
	}

	/**
	 * 前面补零
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static String front(String str, int lenght) {

		for (; str.length() < lenght;) {
			str = "0" + str;
		}
		return str;
	}

	/**
	 * 后面补零
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static String back(String str, int lenght) {

		for (; str.length() < lenght;) {
			str = str + "0";
		}
		return str;
	}


	public static String nullTo(Object o) {
		if (o == null)
			return "";
		return o.toString().trim();
	}

	/**
	 * 获得当前yyyy-MM-dd(年月日)
	 * 
	 * @return
	 * @throws ParseException
	 */
	public static Date getFormatDate() throws ParseException {
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		String dd = myFormatter.format(new Date());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date date = format.parse(dd + " 00:00:00");
		return date;
	}

	/**
	 * 获取该天得最后时刻
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date getendDate(Date date) throws ParseException {
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		String dd = myFormatter.format(date);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return format.parse(dd + " 23:59:59");
	}

	/**
	 * 截取字符串，两个字符当一个汉字，占一位
	 * 
	 * @param text
	 * @param start
	 * @param end
	 * @return
	 */
	public static String textSubstring(String text, int start, int end) {
		int count = 0; // 截图汉字的部分有多少字符
		if (text != null && text.length() > end) {
			int tmpStart = end - 1;
			while (true) {
				if (getCharCount(text, tmpStart, end + count / 2) != 0) {
					tmpStart = end + count / 2 - 1;
					count += getCharCount(text, tmpStart, end + count / 2);
				} else {
					break;
				}
			}
			if (end + count / 2 < text.length()) {
				return text.substring(start, end + count / 2);
			} else {
				return text;
			}

		} else {
			return text;
		}

	}

	/**
	 * 查询字符格式，除去汉字
	 * 
	 * @param text
	 *            文本
	 * @param start
	 *            查询开始处
	 * @param end
	 *            查询结束处
	 * @return
	 */
	public static int getCharCount(String text, int start, int end) {
		Pattern p = Pattern.compile("[\u4E00-\u9FA5]| ");
		String tmp = "";
		int i = 0;
		int j = 0; // 文本的长度
		int count = 0; // 截图汉字的部分有多少字符
		int length = 0;
		if (text != null && text.length() > end) {
			length = text.length();
			tmp = text.substring(start, end);
			j = tmp.length();
			Matcher m = p.matcher(tmp);

			while (m.find()) {
				i++;
			}
			count = j - i;

		}
		return count;
	}

	/**
	 * 替换掉手机号中间四位
	 * 
	 * @param mobile
	 * @return
	 */
	public static String cutMobile(String mobile) {
		if(StringUtils.isBlank(mobile)) {
			mobile = "未绑定";
		} else {
			mobile = mobile.substring(0, 3) + "****" + mobile.substring(7, 11);
		}
		return mobile;
	}
	/**
	 * 只保留电话号码后4位
	 * 
	 * @param mobile
	 * @return
	 */
	public static String cutTel(String tel) {
		if(StringUtils.isBlank(tel)) {
			tel = "未绑定";
		} else {
			tel = tel.substring(0,tel.length()-4) + "****";
		}
		return tel;
	}
	/**
	 * 替换掉邮箱@前若干位
	 * 
	 * @param email
	 * @return
	 */
	public static String cutEmail(String email) {
		if(StringUtils.isBlank(email)) {
			email = "未绑定";
		} else {
			int index = email.indexOf("@");
			if (index > 0) {
				int length = email.substring(0, index).length();
				if (length > 6) {
					email = email.replace(email.substring(index - 4, index),
							"****");
				} else {
					email = email.replace(email.substring(index - 2, index),
							"**");
				}
			}
		}
		return email;
	}
	
	

	/**
	 * 检查pClassName是否符合定义的名称
	 * 
	 * @param pClassName
	 * @return
	 */
	public static boolean checkpClassName(String pClassName) {
		
		if (pClassName.equals("liClas1")) {
			return true;
		} else if (pClassName.equals("liClas2")) {
			return true;
		} else if (pClassName.equals("liClas3")) {
			return true;
		} else if (pClassName.equals("liClas4")) {
			return true;
		} else if (pClassName.equals("liClas5")) {
			return true;
		} else if (pClassName.equals("liClas6")) {
			return true;
		} else if (pClassName.equals("liClas7")) {
			return true;
		} else if (pClassName.equals("liClas8")){
			return true;
		}else if ("".equals(pClassName)) {
			return true;
		}else {
			return false;
		}

	}
	/**
	 * 获得长度<skucode>用
	 * @param num
	 * @param len
	 * @return
	 * @throws Exception 
	 */
	public static String getFastenLenth(Long num,int len) throws Exception{
		if(num==null){
			num=0l;
		}
		String nums=num.toString();
		int nowLen=nums.length();
		if(nowLen>len){
			throw new Exception("长度太长");
		}else{
			for(int i=nowLen;i<len;i++){
				nums="0"+nums;
			}
		}
		return nums;
	}
}
